package com.hsy.crm.web.service.asyn.marketprofit;

import com.hsy.crm.web.domain.entity.Message;
import com.hsy.crm.web.domain.entity.StoreProductDetail;
import com.hsy.crm.web.mapper.StoreProductDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 销售单反审核       入库
 */
@Service
public class UnSaleProfitService {

    @Autowired
    StoreProductDetailMapper storeProductDetailMapper;

    @Autowired
    SaleProfitService saleProfitService;

    @Autowired
    ProfitCommonService profitCommonService;

    /**
     * 回滚本次销售利润处理  重算后续销售利润
     * @param message
     * @param storeProductDetails
     */
    public void init(Message message, List<StoreProductDetail> storeProductDetails){
        for (StoreProductDetail storeProductDetail : storeProductDetails){
            this.init(message,storeProductDetail);
        }
    }

    private void init(Message message,StoreProductDetail storeProductDetail){
        profitCommonService.clearOutStoreProductDetail(message,storeProductDetail,Boolean.TRUE);

        //after time re calc  rush_back??
        List<StoreProductDetail> storeProductDetails = storeProductDetailMapper.selectOutByTime(storeProductDetail);
        for( StoreProductDetail outStoreProductDetail : storeProductDetails ){
            profitCommonService.clearOutStoreProductDetail(message,outStoreProductDetail,Boolean.FALSE);
        }

        storeProductDetails = storeProductDetailMapper.selectOutByTime(storeProductDetail);
        for( StoreProductDetail outStoreProductDetail : storeProductDetails ){
            outStoreProductDetail.setCustomerId(message.getCustomerId());
            saleProfitService.calcStoreProductDetailDone(message,outStoreProductDetail);
        }
    }

}
